package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

//    @GetMapping("/emps")
//    public Result getEmps(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize) {
//        log.info("getEmps page:{},pageSize:{}",page,pageSize);
//        PageBean pageBean = empService.page(page,pageSize);
//
//        return Result.success(pageBean);
//    }


    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("getEmps page:{},pageSize:{},name:{},gender:{},begin:{},end:{}", page, pageSize, name, gender, begin, end);
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);

        return Result.success(pageBean);
    }

    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("delete ids:{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping("/emps")
    public Result save(@RequestBody Emp emp) {
        log.info("add emp:{}", emp);
        empService.save(emp);
        return Result.success();
    }

//    @PutMapping("/emps")
//    public Result update(@RequestBody Emp emp) {
//        log.info("update emp:{}", emp);
//        empService.update(emp);
//        return Result.success();
//    }

    @GetMapping("/emps/{id}")

    public Result getEmpById(@PathVariable Integer id) {
        log.info("getEmpById id:{}", id);

        Emp emp = empService.getEmpById(id);
        return Result.success(emp);
    }

    @PutMapping("/emps")
    public Result addEmp(@RequestBody Emp emp) {
        log.info("addEmp emp:{}", emp);
        empService.addEmp(emp);
        return Result.success();
    }
}